package com.platform.api;

import cn.hutool.core.lang.Validator;
import com.platform.annotation.IgnoreAuth;
import com.platform.entity.AnnouncementVo;
import com.platform.service.ApiAnnouncementService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/announcement")
public class ApiAnnouncementController extends ApiBaseAction {

    @Autowired
    private ApiAnnouncementService announcementService;//公告

    @RequestMapping("list")
    @IgnoreAuth
    public Object getAnnList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("limit", size);
        param.put("enabled", 1);
        param.put("sidx", "id");
        param.put("order", "desc");
        Query query = new Query(param);
        List<AnnouncementVo> announcementList = announcementService.queryList(query);
        int total = announcementService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(announcementList, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }

    @RequestMapping("detail")
    @IgnoreAuth
    public Object detail(Integer id) {
        if (Validator.isEmpty(id)) {
            return toResponsFail("请提供id");
        }
        AnnouncementVo announcementVo = announcementService.queryObject(id);
        return toResponsSuccess(announcementVo);
    }

}
